package com.enterprise.projectManager.service.implemention;

import com.enterprise.projectManager.dto.PaySearchConditions;
import com.enterprise.projectManager.model.PayProgress;
import com.enterprise.projectManager.model.Project;
import com.enterprise.projectManager.rope.PayProgressRope;
import com.enterprise.projectManager.service.PayProgressService;
import jakarta.persistence.EntityManager;
import jakarta.persistence.PersistenceContext;
import jakarta.transaction.Transactional;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
@RequiredArgsConstructor
@Transactional
public class PayProgressImpl implements PayProgressService{
    private final PayProgressRope payProgressRope;
    private final CountServiceImpl countService;
    @PersistenceContext
    private EntityManager em;
    @Override
    public PayProgress updatePayProgress(PayProgress payProgress) {
        return payProgressRope.save(payProgress);
    }

    @Override
    public List<PayProgress> listAll() {
        return payProgressRope.findAll();
    }


    @Override
    public PayProgress search(String id) {
        Optional<PayProgress> optionalPayProgress = payProgressRope.findById(id);
        return optionalPayProgress.orElse(new PayProgress());
    }

    @Override
    public PayProgress searchPayProgressByContractName(String contractName) {
        Optional<PayProgress> optionalProblem = payProgressRope.findById(contractName);
        return optionalProblem.orElse(null); // 或者根据实际需求返回其他默认值
    }
    @Override
    public PayProgress searchPayProgressByContractType(String contractType) {
        Optional<PayProgress> optionalProblem = payProgressRope.findById(contractType);
        return optionalProblem.orElse(null); // 或者根据实际需求返回其他默认值
    }
    @Override
    public PayProgress searchPayProgressByFirstCompany(String firstCompany) {
        Optional<PayProgress> optionalProblem = payProgressRope.findById(firstCompany);
        return optionalProblem.orElse(null); // 或者根据实际需求返回其他默认值
    }
    @Override
    public PayProgress searchPayProgressBySecondCompany(String secondCompany) {
        Optional<PayProgress> optionalProblem = payProgressRope.findById(secondCompany);
        return optionalProblem.orElse(null); // 或者根据实际需求返回其他默认值
    }
    @Override
    public PayProgress searchPayProgressByContractState(String contractState) {
        Optional<PayProgress> optionalProblem = payProgressRope.findById(contractState);
        return optionalProblem.orElse(null); // 或者根据实际需求返回其他默认值
    }

    @Override
    public List<PayProgress> filterProgressBySQL(PaySearchConditions paySearchConditions) {
        System.out.println(paySearchConditions);
        return payProgressRope.filterPayProgress(
                paySearchConditions.getName(),
                paySearchConditions.getType(),
                paySearchConditions.getMinCost(),
                paySearchConditions.getMaxCost(),
                paySearchConditions.getFirstCompany(),
                paySearchConditions.getSecondCompany(),
                paySearchConditions.getStatus(),
                paySearchConditions.getPayPlan(),
                paySearchConditions.getPayDate() == null ? null : (paySearchConditions.getPayDate().length == 0 ? null : paySearchConditions.getPayDate()[0]),
                paySearchConditions.getPayDate() == null ? null : (paySearchConditions.getPayDate().length <= 1 ? null : paySearchConditions.getPayDate()[1]),
                paySearchConditions.getSignDate() == null ? null : (paySearchConditions.getSignDate().length == 0 ? null : paySearchConditions.getSignDate()[0]),
                paySearchConditions.getSignDate() == null ? null : (paySearchConditions.getSignDate().length <= 1 ? null : paySearchConditions.getSignDate()[1])
        );
    }

    @Override
    public PayProgress createPayProgress(PayProgress payProgress) {
        if (payProgress.getPayId() == null) payProgress.setPayId(countService.generalPayProgressId());
        return payProgressRope.save(payProgress);
    }


}
